import Vue from "@/main";
// 大写转小写
export const lowerJSONKey = (json) => {
  if (json instanceof Array) {
    for (const key in json) {
      lowerJSONKey(json[key]);
    }
  } else if (json instanceof Object) {
    for (const key in json) {
      // let reg = /[A-Z]+$/;
      if (json[key] instanceof Object || json[key] instanceof Array) {
        lowerJSONKey(json[key]);
      } else {
        // if (reg.test(key)) {
        const lowerKey = key.toLowerCase();
        json[lowerKey] = json[key];
        delete json[key];
        // }
      }
    }
  }
  return json;
};
// 文件查询
export const selectFile = (id) => {
  if (id && id.indexOf('null') === -1) {
    return Vue.$api.utils.getAttachInfo({ id });
  } else {
    return Promise.resolve([])
  }
};
// 计算百分比
export const Percentage = (num, total) => {
  if (!num || !total) {
    return 0;
  }
  return (Math.round(num / total * 10000) / 100.00);// 小数点后两位百分比
}
// 同比计算  同比增长率=（本期数－同期数）÷同期数×100%
export const tbNumber = (num, snum) => {
  const bnum = Number(num)
  const tnum = Number(snum)
  if (!bnum || !tnum) {
    return 0;
  }
  return Math.round((bnum - tnum) / tnum * 10000) / 100
}
// 保留n位小数
export const roundFun = (value, n) => {
  return Math.round(Number(value) * Math.pow(10, n)) / Math.pow(10, n);
}

export const fomatFloat = (value, n) => {
  const f = Math.round(value * Math.pow(10, n)) / Math.pow(10, n);
  let s = f.toString();
  const rs = s.indexOf('.');
  if (rs < 0) {
    s += '.';
  }
  for (let i = s.length - s.indexOf('.'); i <= n; i++) {
    s += "0";
  }
  return s;
}
// 解析获取所有文件集合
export const importAll = (r) => {
  const cache = {}
  // eslint-disable-next-line no-return-assign
  r.keys().forEach(key => cache[key] = r(key))
  return cache;
}
// utils.js
export const transformFontSize = (val, designWidth = 1920) => {
  const clientWidth = window.innerWidth || document.documentElement.clientWidth;
  if (!clientWidth) return val;
  const ratio = clientWidth / designWidth;
  return ratio * val;
}
